package com.aaa.system.controller;


import com.aaa.system.entity.UserRole;
import com.aaa.system.service.IUserRoleService;
import com.aaa.system.vo.Result;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 闫克起
 * @since 2023-06-20
 */
@RestController
@RequestMapping("/system/user-role")
public class UserRoleController {

    @Autowired
    private IUserRoleService userRoleService;
    @GetMapping("confirm")
    public Result confirm(String userId, String[] roleIds){
        //1.根据用户id删除对应的角色记录
        UpdateWrapper<UserRole> wrapper=new UpdateWrapper<>();
        wrapper.eq("user_id",userId);
        userRoleService.remove(wrapper);
        //2.批量添加
//        List<UserRole> userRoleList=new ArrayList<>();
//        for(String roleId:roleIds){
//             UserRole userRole=new UserRole();
//             userRole.setUserId(userId);
//             userRole.setRoleId(roleId);
//             userRole.setGmtCreate(LocalDateTime.now());//
//             userRole.setIsDeleted(0);
//             userRole.setGmtModified(LocalDateTime.now());//
//            userRoleList.add(userRole);
//        }
        List<UserRole> userRoleList = Arrays.stream(roleIds)
                .map(roleId -> new UserRole(roleId, userId, 0, LocalDateTime.now(), LocalDateTime.now()))
                .collect(Collectors.toList());
        userRoleService.saveBatch(userRoleList);

        return new Result(200,"分配角色成",null);
    }
}
